<?php
//公共函数库
/**
 * 字符串加密、解密函数
 *
 *
 * @param	string	$txt		字符串
 * @param	string	$operation	ENCODE为加密，DECODE为解密，可选参数，默认为ENCODE，
 * @param	string	$key		密钥：数字、字母、下划线
 * @param	string	$expiry		过期时间
 * @return	string
 */
function encry_code($string, $operation = 'ENCODE', $key = '', $expiry = 0) {
    $ckey_length = 4;
    $key = md5($key != '' ? $key : config('encry_key'));
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';

    $cryptkey = $keya . md5($keya . $keyc);
    $key_length = strlen($cryptkey);

    $string = $operation == 'DECODE' ? base64_decode(strtr(substr($string, $ckey_length), '-_', '+/')) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
    $string_length = strlen($string);

    $result = '';
    $box = range(0, 255);

    $rndkey = array();
    for ($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }

    for ($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }

    for ($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }

    if ($operation == 'DECODE') {
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc . rtrim(strtr(base64_encode($result), '+/', '-_'), '=');
    }
}


/**
 * 远程请求函数
 * @author fanhe   		        2018-6-1
 * @param string $url           请求网址
 * @param array $post           post参数
 * @param string $headers       header
 * @param string $user_agent    user_agent
 * @return $response
 */
function vcurl($url, $post = array(),$headers = array(),$user_agent = false) {
    $ci = curl_init();
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ci, CURLOPT_ENCODING, "");
    curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ci, CURLOPT_HEADER, FALSE);
    if($user_agent == true){
        curl_setopt($ci, CURLOPT_USERAGENT, 'Mozilla/5.0');
    }else{
        curl_setopt($ci, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    }
    if (!empty($post)) {
        curl_setopt($ci, CURLOPT_POST, TRUE);
        curl_setopt($ci, CURLOPT_POSTFIELDS, $post);
    }
    if(!empty($headers)){
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers );
    }
    curl_setopt($ci, CURLOPT_TIMEOUT,60);
    curl_setopt($ci, CURLOPT_URL, $url );
    curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE );

    $response = curl_exec($ci);

    if (false) {
        echo "=====post data======\r\n";
        var_dump($post);

        echo "=====headers======\r\n";
        var_dump($headers);

        echo '=====request info====='."\r\n";
        var_dump( curl_getinfo($ci) );

        echo '=====response====='."\r\n";
        var_dump( $response );
    }
    curl_close ($ci);
    return $response;
}

/**
 * 公共上传方法
 * @param $key  上传文件的key
 */
function upload($key , $path = ''){
    // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file($key);
    // 移动到框架应用根目录/public/uploads/ 目录下
    if(!$path){
        $path = config('upload_file.default');
    }
    if($file){
        $info = $file->rule('uniqid')->move('.'.$path);
        if($info){
            return ['url'=>$path.$info->getFilename()];
        }else{
            // 上传失败获取错误信息
            return $file->getError();
        }
    }
}

//是否绑卡
function tie_card($import_type) {
    if ($import_type > 0) {
        return '是';
    }
    return '否';
}